home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Niklas Mellin <niklas.mellin@sto.fdata.se>
- Subject: Re: Why don't you use garbage collection
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <316D291F.465D@sto.fdata.se>
- Date: Thu, 11 Apr 1996 15:45:35 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <4kiai0$mjd@dfw-ixnews8.ix.netcom.com>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0 (WinNT; I)
- Organization: WM-data F÷rsvarsdata AB, Sweden
-
- Giuliano Carlini wrote:
- >
- > I'm a long time proponent of using garbage collection in C and C++
- > programs, and I'm curious:
- > - How many others are there?
-
- Not me (usually).
-
- > - Why don't most C/C++ programmers use it?
-
- I don't use it mostly because I tend to put almost every object I use
- on the stack and not on the heap. The heap usage I hide in low level
- classes, that typically have some kind of reference counter, and takes
- care of the deletions.
-
- Sometimes it is necessary to put a high level object on the heap, but
- those cases are so rare that it is not worth the trouble implementing
- a garbage collector. And personally I usually don't have any problem
- remembering the delete or delete[] in those cases. When there are
- resource leaks in my programs it is most often due to forgetting to
- return some other resource than memory, in the destructor of an object.
-
- > I'm particularly interested in finding out why most C/C++ don't use it.
- > While I have my own theories - which I'll describe below - I'm
- > interested
- > in finding out more directly from those who are against it. I've spent
- > a
- > good part of the past 4 years trying to convince people to use it, and
- > I've got a handle on why the small group of people I associate with are
- > not using it, but I'd like to find out more about why the C/C++
- > community
- > as a whole does not.
-
- I am not against it, it is just that I think the stack suits my needs
- better. I agree though that there have been cases I have wished I had
- a garbage collector. But I don't think a garbage collector fits into
- the language as well as in Smalltalk or Lisp. There is just too much
- you can do with pointers, so implementing a garbage collecting C++
- is anything but trivial, and restricting the usage of pointers would
- surely not be biased towards the C++ culture.
-
- > I hope that with this information, I'll be able to perfect a more
- > convincing presentation for why the default should be to use garbage
- > collection, and for why explicit calls to free/delete should be used
- > only
- > in the rare cases for which garbage collection is inappropriate.
-
- There are very few calls to new and delete in the code I write, except
- in the implementation of low level classes.
-
- > What follows is my belief for why garbage collection is so little used.
- > Feel free to respond to anything I say below, but please, first respond
- > to the questions above. I believe that most people don't use garbage
- > collection because either they:
- > - don't know what it is
-
- I think most professional programmers at least have an idea what it is.
- Everyone that is majoring in computer science today I believe sees it
- in school.
-
- > - don't know it can be used with C/C++.
-
- I haven't seen any implementation that I really liked, but then I
- haven't activly searched for one either.
-
- > - are misinformation
- > - are biased against it by the C/C++ culture
- > In my experience, most C/C++ programmers either don't know what garbage
- > collection is, or don't know that it can be used with C/C++. After all,
- > no major C/C++ compiler includes a garbage collector. At least, as far
- > as I know. I hope I'm wrong, and that someone can correct me. But even
- > after, I tell them what it is, and that it can be used with C++, almost
- > everyone still rejects it.
-
- No major C/C++ compiler includes a garbage collector, because it is not a
- part of the C++ language. If you want a garbage collector you should go out
- looking for a class library that implements it, not for a compiler. A C++
- compiler that do garbage collection when you forget delete wouldn't be a
- C++ compiler.
-
- > At first, most offer technical reasons for rejecting it. Almost all are
- > based on misinformation, since garbage collection is usable and
- > benificial
- > for the vast majority of systems. The most often mentioned is that
- > garbage
- > collection is too inefficient. Either it uses too much CPU, or too much
- > memory. Fortunately, this is easy to rebut. The research literature
- > gives
- > ample evidence that modern garbage collectors for C/C++ use around the
- > same CPU time as programs that explicitly call delete/free. My personal
- > experience is that typical programs that call delete/free use about the
- > same amount of memory as garbage collected programs. In order to impose
- > the predictability needed to permit calls to delete, they are forced to
- > do deep copies of complex data structures. This uses a great deal of
- > memory. Garbage collected programs need use only a single copy of the
- > data, and this compensates for the additional memory taken up by
- > unreachable objects.
-
- My philosophy is that reference counting together with copy on write
- solves this problems just as good garbage collection in most cases.
-
- > Another reason often given is that garbage collection introduces long
- > pause times that are acceptable to users. This was certainly true of
- > garbage collectors at one time, but modern collectors allow the client
- > program to employ several techniques to eliminate pause times. The most
- > sophisticated is to operate in a separate thread. Thus while the
- > garbage
- > collection thread is executing, the user can still interact with the
- > system. Another technique is to run the collector when the user has
- > been
- > idle for X seconds. If the user has been idle so far, he is likely to
- > remain idle. So, the collector runs while the user is making no demands
- > on
- > the system. If he happens to resume interating with the program in the
- > middle of the collection, the collector detects this, and abandons the
- > collection. The collector can then wait for the next idle period.
- >
- > Other technical reasons are offered, but I'll skip them as this note is
- > already much too long. They apply only to unusual applications that are
- > outside the mainstream. Even most of these reasons are wrong though.
-
- I'd love to take a look at a garbage collector that solves all these
- problems in the way you describe above. If you know one for C++, please
- give me a pointer to it. (I don't want to implement it myself).
-
- > So, we're left with the cultural reasons, and these are I believe the
- > "real" reasons why people reject garbage collection. I can shoot down
- > incorrect technical arguments, but it's tough to dispute culture,
- > although
- > I sure try. The crux of cultural reason against garbage collection is
- > that
- > relying on a garbage collector is "sloppy". The "Code talk" note in the
- > January Byte is a great example of this thinking. This sort of C/C++
- > programmer believes that it is just wrong to not explicitly call
- > delete.
- > That if you don't, your an undisciplined hack. That if you don't,
- > you've
- > turned to the dark side and surely will come to a bad end.
-
- Every language has its own culture. Since good garbage collectors
- in C++ is if not unexcistent, at least very rare, it is just
- natural that not deleting an object is regarded as a bad habit.
-
- I refuse to participate at any religous war between different
- languages. I realize that every language has its strengths and
- weaknesses. I am good at C++, but I know quite a few other
- languages too, and appriciate them for their strengths.
-
- > I'm not sure how to address this. My current argument seems to get them
- > thinking, but isn't immediately convincing. Anyone have any ideas on a
- > better approach. Or perhaps on a better presentation. My argument is
- > that
- > computers are very good at mechanistic, repetitive tasks. And that
- > people
- > are usually pretty bad at them; being mechanistic and repetitive, these
- > tasks are rather boring, which means that our attention often strays.
- > People
- > routinely turn over such tasks to computers. For example, bookkeeping
- > or
- > inventory control. And what else is memory deallocation but a sort of
- > bookkeeping or inventory control. It's just that rather than being for
- > a resource outside of the computer it is for one inside.
-
- So why not do as I do? Write low level template classes that takes care
- of the book keeping, and reuse them, and don't use the heap more than
- necessary, usually it is easier and more efficient to use the stack.
-
- > For anyone who may be wondering, I believe that we should use garbage
- > collection because:
- > - It vastly decreases the number of bugs in programs which use
- > it.
-
- It is easier to debug a reference counting class than to debug a garbage
- collector.
-
- > - It vastly decreases the time to complete programs which use
- > it.
- > - It vastly increases the understandability, reuseability,
- > extensability, and maintainability of programs which use
- > it.
-
- ---
- Niklas Mellin
-